﻿using Mes.Pendant.Common;
using Mes.Pendant.ProcurementWarehousings.Dto;
using Mes.Pendant.ProcurementWarehousings;
using Mes.Pendant.Productionrequisitions;
using Mes.Pendant.ProductionrequisitionService.Dto;
using Mes.Pendant.ProductionrequisitionService.Services;
using Mes.Pendant.Warehousesettings.Dto;
using Mes.Pendant.WarehousesettingServicess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using Mes.Pendant.MaterialManagementService.Dto;
using AutoMapper;
using Mes.Pendant.ArrivalNotices.Dto;
using Mes.Pendant.SupplierManagements;
using CSRedis;
using Mes.Pendant.Common.DtoMapping;

namespace Mes.Pendant.ProductionrequisitionServices
{
    public class ProductionrequisitionServices : CrudAppService<Productionrequisition, ProductionrequisitionDto, int, PagedResultRequestDto, ProductionrequisitionDto>, IProductionrequisitionServices
    {
        private readonly IRepository<Productionrequisition,int> _repository;
        private readonly IRepository<SupplierManagement, int> supplierManagementRepository;

        private readonly Object obj=new Object();
        private readonly CSRedisClient redisClient = new CSRedisClient("localhost:6379");

        public ProductionrequisitionServices(IRepository<Productionrequisition, int> repository, IRepository<SupplierManagement, int> supplierManagementRepository) : base(repository)
        {
            _repository = repository;
            this.supplierManagementRepository = supplierManagementRepository;
        }

        public Task<int> CreateProductionrequisition(ProductionrequisitionDto productionrequisitionDto)
        {
            throw new NotImplementedException();
        }

        public Task<int> DeleteProductionrequisition(int id)
        {
            throw new NotImplementedException();
        }

        public Task<ProductionrequisitionDto> GetProductionrequisitionById(int id)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 显示查询分页成产领料
        /// </summary>
        /// <param name="codes"></param>
        /// <param name="name"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<PagedResult> ProductionrequisitionList(string? codes, string? name, int page, int limit)
        {
            var product = PredicateBuilder.New<Productionrequisition>(true);
            if (!string.IsNullOrWhiteSpace(codes))
            {
                product = product.And(m => m.RequisitionCode == codes);
            }
            if (!string.IsNullOrWhiteSpace(name))
            {
                product = product.And(m => m.RequisitionName == name);
            }
            var productList = _repository.GetListAsync(product).Result;

            PagedResult pagedResult = new PagedResult();
            pagedResult.CurrentPage = page;
            pagedResult.PageSize = limit;
            pagedResult.RowCount = productList.Count();
            pagedResult.PageCount = (int)Math.Ceiling((pagedResult.RowCount) / (double)limit);
            pagedResult.Queryable = productList.OrderBy(m => m.Id).Skip((page - 1) * limit).Take(limit).AsQueryable();
            pagedResult.Queryable = pagedResult.Queryable.MapToList<ProductionrequisitionDto>().AsQueryable();
            return pagedResult;
        }

        public Task<int> UpdateProductionrequisition(ProductionrequisitionDto productionrequisitionDto)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 获取供应商数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<SupplierManagement>> Getsuppernames()
        {
            var typeList = redisClient.Get<List<SupplierManagement>>("typeListKey");
            if(typeList== null)
            {
                lock (obj)
                {
                    if (typeList == null)
                    {
                        typeList = supplierManagementRepository.GetListAsync().Result;
                        var supp = 3000 + new Random().Next(1, 1000);
                        redisClient.Set("typeListKey", typeList, supp);
                    }
                }
            }
            return typeList;
        }
    }
}
